local P = {}


local U = {}
local prefixKey = "prefix"
function U.__index(t, k)
	local prefix = rawget(t.parent, prefixKey)
	if prefix then
		return UnityEngine.PlayerPrefs[t.getFunName](prefix.."_"..k)
	else
		return UnityEngine.PlayerPrefs[t.getFunName](k)
	end
end

function U.__newindex(t, k, v)
	local prefix = rawget(t.parent, prefixKey)
	if prefix then
		UnityEngine.PlayerPrefs[t.setFunName](prefix.."_"..k, v)
	else
		UnityEngine.PlayerPrefs[t.setFunName](k, v)
	end
end






local B = {}
local prefixKey = "prefix"
function B.__index(t, k)
	local prefix = rawget(t.parent, prefixKey)
	if prefix then
		k = prefix.."_"..k
	end

	valid, result = PlayerPrefsX[t.getFunName](k, nil)
	if not valid then
		return valid
	end

	return result
end

function B.__newindex(t, k, v)
	local prefix = rawget(t.parent, prefixKey)
	if prefix then
		k = prefix.."_"..k
	end

	if not v then
		UnityEngine.PlayerPrefs.DeleteKey(k)
	else
		PlayerPrefsX[t.setFunName](k, v)
	end
end





local C = {}
local prefixKey = "prefix"
function C.__index(t, k)
	local prefix = rawget(t.parent, prefixKey)
	if prefix then
		k = prefix.."_"..k
	end

	return PlayerPrefsX[t.getFunName](k)

end

function C.__newindex(t, k, v)
	local prefix = rawget(t.parent, prefixKey)
	if prefix then
		k = prefix.."_"..k
	end

	if not v then
		UnityEngine.PlayerPrefs.DeleteKey(k)
	else
		PlayerPrefsX[t.setFunName](k, v)
	end
end

local D = {}
local prefixKey = "prefix"
function D.__index(t, k)
	local prefix = rawget(t.parent, prefixKey)
	if prefix then
		k = prefix.."_"..k
	end

	return PlayerPrefs[t.getFunName](k)
end



function P.New(prefix)
	local temp = {}
	if prefix then
		temp.prefix = tostring(prefix)
	end



	local int = {}
	int.parent = temp
	int.setFunName = "SetInt"
	int.getFunName = "GetInt"
	setmetatable(int, U)

	local float = {}
	float.parent = temp
	float.setFunName = "SetFloat"
	float.getFunName = "GetFloat"
	setmetatable(float, U)


	local string = {}
	string.parent = temp
	string.setFunName = "SetString"
	string.getFunName = "GetString"
	setmetatable(string, U)

	local vector3 = {}
	vector3.parent = temp
	vector3.setFunName = "SetVector3"
	vector3.getFunName = "GetVector3"
	setmetatable(vector3, B)

	local bool = {}
	bool.parent = temp
	bool.setFunName = "SetBool"
	bool.getFunName = "GetBool"
	setmetatable(bool, C)

	local hasKey = {}
	hasKey.parent = temp
	hasKey.setFunName = "HasKey"
	setmetatable(hasKey, D)

	-- local bool = {}
	-- function bool.__index(t, k)
	-- 	return int[k] ~= 0
	-- end
	-- function bool.__newindex(t, k, v)
	-- 	int[k] = v and 1 or 0
	-- end
	-- setmetatable(bool, bool)

	temp.int = int
	temp.float = float
	temp.string = string
	temp.bool = bool
	temp.vector3 = vector3
	temp.hasKey = hasKey

	return temp
end

Setting = P

